package ch.bfh.abch.rems.sender.tinkerforge;

import org.apache.log4j.Logger;

import ch.bfh.abch.rems.MulticastMessageType;
import ch.bfh.abch.rems.sender.MulticastSender;
import ch.bfh.abcm.rems.sensors.tinkerforge.TFSensorType;
import ch.bfh.abcm.rems.tinkerforge.TFClusterNodeAverage;

import com.google.gson.Gson;

public class TFMulticastSender extends MulticastSender {
    
    private static Logger logger = Logger.getLogger("play");

    public TFMulticastSender(String multicastAdress, int receiverPort) {
        super(multicastAdress, receiverPort);
    }

    public void sendCLusterNodeAverage(int value, String clusterNodeID, TFSensorType sensorType) {
        sendDatagramPacket(getByteArrayFromTFCNA(value, clusterNodeID, sensorType));
    }
    
    private byte[] getByteArrayFromTFCNA(int value, String clusterNodeID, TFSensorType sensorType){
        logger.info("Create clusterNodeAverageObject as String with Gson");
        TFClusterNodeAverage clusterNodeAverage = new TFClusterNodeAverage(value, clusterNodeID, sensorType);
        String cnaString = new Gson().toJson(clusterNodeAverage);
        logger.debug("SendData: "+cnaString);
        return getMulticastMessageByteArray(MulticastMessageType.TF_NODE_AVERAGE, cnaString);
    }
}
